﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace _MetalSlug.Form.SelectCharScreen
{
    public class CharacterDisplayer : VisibleGameEntity
    {
        String name;
        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        Texture2D mainTexture;
        public Texture2D MainTexture
        {
            get { return mainTexture; }
            set { mainTexture = value; }
        }

        Vector2 positionOfCurrentPlayer;
        public Vector2 PositionOfCurrentPlayer
        {
            get { return positionOfCurrentPlayer; }
            set { positionOfCurrentPlayer = value; }
        }

        Vector2 Scale;
        Vector2 PositionOfMainTexture;
        Vector2 NextFrame;

        List<MySprite> listDisplayChosen;
        public List<MySprite> ListDisplayChosen
        {
            get { return listDisplayChosen; }
            set { listDisplayChosen = value; }
        }

        public enum STATE
        {
            DISABLE,
            ACTIVE,
            CHOSEN
        }

        STATE currentState;

        public STATE CurrentState
        {
            get { return currentState; }
            set 
            { 
                currentState = value;
                PositionOfMainTexture = NextFrame * (int)CurrentState;
            }
        }

        MyCover Cover;
        
        public CharacterDisplayer(String HeroName, String pathMainTexture, String pathCover, Vector2 newSize)
        {
            Name = HeroName;
            MainTexture = GameUtil.LoadTexture(pathMainTexture);
            Cover = new MyCover(pathCover);
            Size = newSize;
            ListDisplayChosen = new List<MySprite>();
        }

        public void SetTopLeft(Vector2 newTopLeft, Vector2 scale)
        {
            TopLeft = newTopLeft;
            Scale = scale;
            NextFrame = new Vector2(0, (int)(MainTexture.Height * 1.0 / 3));
            PositionOfCurrentPlayer = new Vector2((31 + 64) * Scale.X, 33 * Scale.Y) + TopLeft;
            Cover.SetTopLeft(TopLeft,Size);
            foreach(MySprite DisplayChosen in ListDisplayChosen)
            {
                Vector2 newSize =  new Vector2(DisplayChosen.Textures[0].Width,DisplayChosen.Textures[0].Height)*Scale;
                DisplayChosen.Size = newSize;
            }
        }

        public void ResetPositionCover()
        {
            Cover.SetTopLeft(TopLeft, Cover.Size); 
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(MainTexture, 
                new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)Size.X, (int)Size.Y),
                new Rectangle((int)PositionOfMainTexture.X, (int)PositionOfMainTexture.Y, (int)MainTexture.Width, (int)(MainTexture.Height * 1.0 / 3)), 
                Color.White);

            Cover.Draw(spriteBatch);
            //spriteBatch.Draw(CoverTexture, new Rectangle((int)PositionOfCover.X, (int)PositionOfCover.Y, (int)Size.X, (int)Size.Y), Color.White);
        }

        public override void Update(GameTime gameTime)
        {
            Cover.Update(gameTime);
            foreach(MySprite Sprite in ListDisplayChosen)
            {
                Sprite.Update(gameTime);
            }
        }

        public void SetSpeedCover(Vector2 speed)
        {
            Cover.DeltaCloseCover = speed;
        }

        public Boolean isCanChoose()
        {
            if(Cover.isOpenComplete())
            {
                return true;
            }
            return false;
        }

        public Boolean isCanClose()
        {
            if (Cover.isCloseComplete())
            {
                return true;
            }
            return false;
        }

        public void CloseCover()
        {
            Cover.CurrentState = MyCover.STATE.CLOSE;
        }

        public void OpenCover()
        {
            Cover.CurrentState = MyCover.STATE.OPEN;
        }
    }
}
